﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Paradox.Models;
using Paradox.Models.DataModels;
namespace Paradox.Models.DataModels
{
    
    //class for setting up a strongly typed class for View data  



    //returns named IQueryable datasets 

    public class StoryRepository
    {
    
        internal RSSDataContext db = new RSSDataContext();
        

        internal IQueryable<Story> GetAllStories()
        {

            return db.Stories;

        }


        internal IQueryable<Story> GetAllPublishedStories()
        {
            return from story in db.Stories
                   where story.publishToRSS == true
                   orderby story.posted_on descending
                   select story;
        }


        internal IQueryable<Story> GetRecentStories(int? quantity)
        {
            var results = from stories in db.Stories
                          where stories.publishToRSS == true
                          orderby stories.posted_on descending
                          select stories;

            return results.Take(quantity ?? 10);
        }


        internal IQueryable<Story> GetRecentStories()
        {
            return from stories in db.Stories
                   where stories.publishToRSS == true
                   orderby stories.posted_on descending
                   select stories;
        }



        internal IQueryable<Comment> GetCommentsForStory(int id)
        {
            return from comments in AllComments()
                   where comments.story_id == id
                   orderby comments.comment_posted_on descending
                   select comments;


        }
        internal IQueryable<Comment> AllComments()
        {
            return db.Comments;
        }

        internal IQueryable<Story> getStory(int id)
        {
            var dbStory = from story in db.Stories
                          where story.id == id
                          select story;

            return dbStory;
        }





        internal IQueryable<Story> getFullStory(int id)
        {
            var story = from fullstory in db.Stories
                        where fullstory.id == id
                        select fullstory;
            return story.Take(1);
        }





        internal IQueryable<Tag> getTags(int id)
        {
            return from tags in db.Tags
                   where tags.storyid == id
                   select tags;
        }

        internal void PostNewStory(Story story)
        {
            db.Stories.InsertOnSubmit(story);
            
        }





        internal void NewTag(Tag tag)
        {
            db.Tags.InsertOnSubmit(tag);
            
        }

        //Persistance

        internal void Save()
        {
            db.SubmitChanges();
        }



    }
}